﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;

namespace MarginTrading.Forms
{
    public partial class EditConfig : Form
    {
        string sConfigName = "";
        string sConfigValue = "";

        public EditConfig(string name, string value)
        {
            sConfigName = name;
            sConfigValue = value;
            InitializeComponent();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (validateInputs() == false)
            {
                return;
            }

            Config config = new Config();

            config.Name = sConfigName;
            config.Value = textBoxValue.Text;

            try
            {
                ConfigServices.Update(config);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Failed to update configuration\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            MessageBox.Show("Configuration updated successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Close();
        }

        private bool validateInputs()
        {
            if (textBoxValue.Text == "")
            {
                MessageBox.Show("Enter new configuration value", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            return true;
        }

        private void EditConfig_Load(object sender, EventArgs e)
        {
            textBoxName.Text = sConfigName;
            textBoxValue.Text = sConfigValue;
        }
    }
}
